home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / UndoableEditSupport.java < prev    next >
Text File  |  1998-06-30  |  3KB  |  124 lines

  1. /*
  2.  * @(#)UndoableEditSupport.java    1.9 97/08/15
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.undo;
  22.  
  23. import com.sun.java.swing.event.*;
  24. import java.util.*;
  25.  
  26. /**
  27.  */
  28. public class UndoableEditSupport {
  29.     protected int updateLevel;
  30.     protected CompoundEdit compoundEdit;
  31.     protected Vector listeners;
  32.     protected Object realSource;
  33.  
  34.     public UndoableEditSupport() {
  35.     this(null);
  36.     }
  37.  
  38.     public UndoableEditSupport(Object r) {
  39.     realSource = r == null ? this : r;
  40.     updateLevel = 0;
  41.     compoundEdit = null;
  42.     listeners = new Vector();
  43.     }
  44.  
  45.     public synchronized void addUndoableEditListener(UndoableEditListener l) {
  46.     listeners.addElement(l);
  47.     }
  48.  
  49.     public synchronized void removeUndoableEditListener(UndoableEditListener l)
  50.     {
  51.     listeners.removeElement(l);
  52.     }
  53.  
  54.     /**
  55.      * Called only from postEdit and endUpdate. Calls
  56.      * undoableEditHappened in all listeners. No synchronization
  57.      * is performed here, since the two calling methods are
  58.      * synchonized.
  59.      */
  60.     protected void _postEdit(UndoableEdit e) {
  61.     UndoableEditEvent ev = new UndoableEditEvent(realSource, e);
  62.     Enumeration cursor = listeners.elements();
  63.     while (cursor.hasMoreElements()) {
  64.         ((UndoableEditListener)cursor.nextElement()).
  65.         undoableEditHappened(ev);        
  66.     }
  67.     }
  68.     
  69.     /**
  70.      * DEADLOCK WARNING: Calling this method may call undoableEditHappened
  71.      * in all listeners.  It is unwise to call this method from one
  72.      * of its listeners.
  73.      */
  74.     public synchronized void postEdit(UndoableEdit e) {
  75.     if (updateLevel == 0) {
  76.         _postEdit(e);
  77.     } else {
  78.         // PENDING(rjrjr) Throw an exception if this fails? 
  79.         compoundEdit.addEdit(e);
  80.     }
  81.     }
  82.  
  83.     public int getUpdateLevel() {
  84.     return updateLevel;
  85.     }
  86.  
  87.     public synchronized void beginUpdate() {
  88.     if (updateLevel == 0) {
  89.         compoundEdit = createCompoundEdit();
  90.     }
  91.     updateLevel++;
  92.     }
  93.  
  94.     /**
  95.      * Called only from beginUpdate. Exposed here for subclasses' use
  96.      */
  97.     protected CompoundEdit createCompoundEdit() {
  98.     return new CompoundEdit();
  99.     }
  100.  
  101.     /**
  102.      * DEADLOCK WARNING: Calling this method may call undoableEditHappened
  103.      * in all listeners.  It is unwise to call this method from one
  104.      * of its listeners.
  105.      */
  106.     public synchronized void endUpdate() {
  107.     updateLevel--;
  108.     if (updateLevel == 0) {
  109.         compoundEdit.end();
  110.         _postEdit(compoundEdit);
  111.         compoundEdit = null;
  112.     }
  113.     }
  114.     
  115.     public String toString() {
  116.     return super.toString() +
  117.         " updateLevel: " + updateLevel +
  118.         " listeners: " + listeners +
  119.         " compoundEdit: " + compoundEdit;
  120.     }
  121. }
  122.  
  123.  
  124.